package com.example.oracle;

import com.example.mapper.OracleMapper;
import com.example.pojo.OracleTable;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Oracle2Mysql {
    private String ftlPath = "D:\\code\\learning-demo\\common-demo\\src\\main\\resources\\templates\\ftl\\";

    @Autowired
    private OracleMapper oracleMapper;

    public void generateMysql(String tableName) {
        System.out.println("---------------------------------");
        System.out.println("---------------------------------");
        tableName = tableName.toUpperCase();

        Map<String, Object> param = new HashMap<>();

        // oracle的字段
        List<Map<String, Object>> listOracleTableField = this.oracleMapper.listTableField(tableName);
        // mysql简化后的字段
        List<Map<String, Object>> listTableField = new ArrayList<>();
        for (int i = 0; listOracleTableField != null && i < listOracleTableField.size(); i++) {
            Map<String, Object> oracleField = listOracleTableField.get(i);

            String columnName = (String) oracleField.get("COLUMN_NAME");
            String dataType = (String) oracleField.get("DATA_TYPE");
            int dataLength = Integer.parseInt(oracleField.get("DATA_LENGTH").toString());
            int dataPrecision = oracleField.get("DATA_PRECISION") == null ? 0 : Integer.parseInt(oracleField.get("DATA_PRECISION").toString());
            int dataScale = oracleField.get("DATA_SCALE") == null ? 0 : Integer.parseInt(oracleField.get("DATA_SCALE").toString());


            String comments = (String) oracleField.get("COMMENTS");
            String mysqlDataType = "";
            if ("VARCHAR2".equals(dataType) && dataLength <=500) {
                mysqlDataType = "varchar(";
                mysqlDataType += dataLength;
                mysqlDataType += ")";
            }if ("VARCHAR2".equals(dataType) && dataLength > 500) {
                mysqlDataType = "text";
            } else if ("NUMBER".equals(dataType)) {
                if (dataScale > 0) {
                    mysqlDataType = "decimal(";
                    mysqlDataType += dataPrecision;
                    mysqlDataType += ",";
                    mysqlDataType += dataScale;
                    mysqlDataType += ")";
                } else if (dataPrecision <= 5) {
                    mysqlDataType = "int(";
                    mysqlDataType += dataPrecision;
                    mysqlDataType += ")";
                } else {
                    mysqlDataType = "bigint(";
                    mysqlDataType += dataPrecision;
                    mysqlDataType += ")";
                }
            } else if ("CLOB".equals(dataType)) {
                mysqlDataType = "text";
            }


            Map<String, Object> mysqlField = new HashMap<>();
            mysqlField.put("columnName", columnName.toLowerCase());
            mysqlField.put("mysqlDataType", mysqlDataType);
            mysqlField.put("comments", comments);

            listTableField.add(mysqlField);
        }
        // 表信息
        OracleTable table = this.oracleMapper.getTableByName(tableName);

        param.put("table", table);
        param.put("listTableField", listTableField);

        String content = this.exe(param);

        System.out.println(content);
        System.out.println("");
        System.out.println("");
    }

    /**
     * 生成mysql建表语句
     */
    public String exe(Map<String, Object> root) {

        StringWriter writer = null;
        try {
            writer = new StringWriter();
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_26);

            // 配置模板位置
            cfg.setDirectoryForTemplateLoading(new File(ftlPath));
            cfg.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_26));

            Template temp = cfg.getTemplate("oracle2mysql.ftl");
            // 生成文件的位置
            temp.process(root, writer);

            return new String(writer.getBuffer());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }
}
